<?php
namespace app\common\service;

use think\Model;

class MagazineService extends BaseService
{

    /**
     * 获取内容页面列表
     * @param $sid
     * @param $name
     * @param $p
     * @param $pageSize
     * @param $template 1代表获取模版店铺内容库列表
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getList($sid, $name, $status, $p, $pageSize, $template = 0){
        $where = [];
        $where['status'] = $status;
        if(!empty($name)){
            $where['name'] = ['like', "%$name%"];
        }

        //判断是否为获取模版店铺内容库列表
//        $where['sid'] = $template == 1 ? config('template_sid') : $sid;
        if($template == 1){
            $where['sid'] = config('template_sid');
            $field = 'mid,name,title,create_time';
            $academy_id = db('store')->where(['store_id' => $sid])->value('academy_id');
            $where['academy_id'] = $academy_id;
        } else {
            $where['sid'] = $sid;
            $field = 'mid,name,title,create_time,status';
        }

        $list_count = db('magazine')->where($where)->count();
        $list = db('magazine')
                ->field($field)
                ->where($where)
                ->order('create_time DESC')
                ->limit(($p - 1)*$pageSize, $pageSize)
                ->select();
        if($list){
            foreach($list as $k => $v){
                $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $list[$k]['name'] = $v['name'] ? $v['name'] : $v['title'];

                if($template == 1){
                    //查看是否引用模版店铺内容库
                    $quote_status = db('magazine')->where(['sid' => $sid, 'origin_mid' => $v['mid'], 'status' => ['<>', -1]])->find();
                    $list[$k]['quote_status'] = $quote_status ? 1 : 0;
                }
            }
        }
        return ['content' => $list,'page' =>['count' =>$list_count]];
    }

    /**
     * 上架/下架页面
     * @param $sid
     * @param $mid
     * @param $status
     * @return array
     */
    public function changeStatus($sid, $mid, $status){
        $info = db('magazine')->where(['sid' => $sid,'mid' => $mid,'status' => ['<>', -1]])->find();
        if(empty($info)){
            return ['status' => 0, 'msg' => '不存在此页面'];
        }

        $result = db('magazine')->where(['sid' => $sid, 'mid' => $mid])->setField('status', $status);
        return $result === false ? ['status' => 0, 'msg' => '修改失败'] : ['status' => 1, 'msg' => '修改成功'];
    }

    /**
     * 获取页面详情
     * @param $sid
     * @param $mid
     * @return array|false|\PDOStatement|string|Model
     */
    public function getDetail($sid, $mid, $status = false){
        $where = [
            'sid' => $sid,
            'mid' => $mid,
            'status' => $status == true ? 1 : ['<>', -1]
        ];
        $detail = db('magazine')->where($where)->find();
        if(!$detail){
            return false;
        }

        $cate_list = [];
        if($detail['category']){
            $category = explode(',', $detail['category']);
            $cate_list = db('magazine_category')
                ->field('cate_id,name')
                ->where([
                    'cate_id' => ['in', $category],
                    'status' => ['<>', -1]])
                ->select();
        }

        $detail['name'] = $detail['name'] ? $detail['name'] : $detail['title'];

        $origin_content = json_decode(htmlspecialchars_decode($detail['content']), 1);
        // 兼容以前的代码数据
        if($origin_content && isset($origin_content['LModules'])){
            $detail['content'] = [];
        } else {
            $detail['content'] = json_decode($detail['content'], 1);
        }


        $detail['category'] = $cate_list;

        return $detail;
    }




    /**
     * 新增内容页面
     * @param $sid
     * @param array $postParam
     * @return int|string
     */
    public function saveMagazine($sid, $mid = 0, $postParam = []){
        $data = [];
        $data['name'] = $postParam['name'];
        $data['title'] = $postParam['title'];
        $data['view_pic'] = $postParam['view_pic'] ?? '';
        $data['describe'] = $postParam['describe'] ?? '';
        $data['show_bottom_menu'] = $postParam['show_bottom_menu'] ?? 0;
        $data['show_subscribe_bar'] = $postParam['show_subscribe_bar'] ?? 0;
        $data['content'] = json_encode($postParam['content'], JSON_UNESCAPED_UNICODE);
        $data['bg_color'] = $postParam['bg_color'] ?? '';
        $data['category'] = $postParam['category'] ?? '';

        /*if(isset($postParam['category']) && !empty($postParam['category'])){
            $data['category'] = $postParam['category'];
            if($mid == 0){
                $category = explode($postParam['category'], 1);
                //页面分类统计页面数量自增1
                db('magazine_category')->where(['sid' => $sid, 'cate_id' => ['in', $category]])->setInc('count',1);
            }
        }*/

        if ($mid > 0){
            $data['update_time'] = time();
            return db('magazine')->where(['sid' => $sid, 'mid' => $mid])->update($data);
        } else {
            $data['sid'] = $sid;
            $data['status'] = 0;
            $data['create_time'] = time();
            $data['academy_id'] = $this->isTemplateStore($sid) == true ? 1 : 0;  //模版店铺创建，目前默认学院ID为1
            return db('magazine')->insertGetId($data);
        }
    }


    /**
     * 保存内容页面自定义内容
     * @param $sid
     * @param $mid
     * @param $content
     * @return int
     */
    public function saveMagazineContent($sid, $mid, $content){
        return db('magazine')->where(['sid' => $sid, 'mid' => $mid])->setField('content', $content);
    }


    /**
     * 保存公用模块内容
     * @param $sid
     * @param $mid
     * @param $common_content
     * @return int
     */
    public function saveMagazineCommonContent($sid, $mid, $common_content){
        return db('magazine_common')->where(['sid' => $sid])->setField('common_content', $common_content);
    }

    /**
     * 获取公用模块内容
     * @param $sid
     * @return mixed
     */
    public function getMagazineCommonContent($sid){
        $data['content'] = db('magazine_common')->where(['sid' => $sid])->value('common_content');
        return $data;
    }


    /**
     * 删除内容页面
     * @param $sid
     * @param $mid
     * @return int
     */
    public function deleteMagazine($sid, $mid){
        return db('magazine')->where(['sid' => $sid, 'mid' => $mid])->setField('status', -1);
    }

}